﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Milestone.Model.Parties;
using Raven.Abstractions.Indexing;
using Raven.Client.Indexes;
using Topics.Radical.Windows.Behaviors;

namespace Milestone.Boot.Indexes
{
	public class Parties_FullTextSearchIndex : AbstractMultiMapIndexCreationTask<Parties_FullTextSearchIndex.PartySimpleView>
	{
		public class PartySimpleView 
		{
			public String Id { get; set; }
			public String DisplayName { get; set; }

			public override string ToString()
			{
				return this.DisplayName;
			}
		}

		public Parties_FullTextSearchIndex()
		{
			this.AddMap<Company>( docs => from doc in docs 
										  select new 
										  {
											  doc.Id,
											  DisplayName = doc.CompanyName
										  } );

			this.AddMap<Person>( docs => from doc in docs
										  select new
										  {
											  doc.Id,
											  DisplayName = doc.FirstName + " " + doc.LastName
										  } );

			this.Index( s => s.DisplayName, FieldIndexing.Analyzed );
			this.Store( s => s.DisplayName, FieldStorage.Yes );
		}
	}
}
